#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void BubbleSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int flag = 1;
		for (int j = 0; j < n - 1 - i; j++)
		{
			if (a[j] > a[j + 1])
			{
				int tmp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = tmp;
				flag = 0;
			}
		}
		if (flag == 1)
			break;
	}
}

void swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

void SelectSort(int* a, int n)
{
	/*for (int i = 0; i < n - 1; i++)
	{
		for (int j = i + 1; j < n; j++)
		{
			if (a[j] < a[i])
			{
				int tmp = a[j];
				a[j] = a[i];
				a[i] = tmp;
			}
		}
	}*/
	int begin = 0, end = n - 1;
	while (begin < end)
	{
		int mini = begin, maxi = begin;
		for (int i = begin + 1; i <= end; i++)
		{
			if (a[i] > a[maxi])
				maxi = i;
			if (a[i] < a[mini])
				mini = i;
		}
		swap(&a[mini], &a[begin]);
		if (maxi == begin)
			maxi = mini;
		swap(&a[maxi], &a[end]);
		begin++;
		end--;
	}
}

void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (tmp < a[end])
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
					break;
			}
			a[end + gap] = tmp;
		}
	}
}

void InsertSort(int* a, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			if (tmp < a[end])
			{
				a[end + 1] = a[end];
				end -= 1;
			}
			else
				break;
		}
		a[end + 1] = tmp;
	}
}

void AdjustDown(int* a, int n, int parent)
{
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child] < a[child + 1])
			child++;
		if (a[child] > a[parent])
		{
			swap(&a[child], &a[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
			break;
	}
}

void DuiSort(int* a, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		end--;
	}
}

int Getmid(int* a, int begin, int end)
{
	int midi = (begin + end) / 2;
	if (a[begin] < a[midi])
	{
		if (a[midi] < a[end])
			return midi;
		else
		{
			if (a[begin] > a[end])
				return begin;
			else
				return end;
		}
	}
	else
	{
		if (a[end] > a[begin])
			return begin;
		else
		{
			if (a[end] < a[midi])
				return midi;
			else
				return end;
		}
	}
}

int PartSort1(int* a, int begin, int end)
{
	int mini = Getmid(a, begin, end);
	swap(&a[begin], &a[mini]);
	int keyi = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && a[right] >= a[keyi])
			right--;
		while (left < right && a[left] <= a[keyi])
			left++;
		swap(&a[left], &a[right]);
	}
	swap(&a[keyi], &a[left]);
	return left;
}

int PartSort2(int* a, int begin, int end)
{
	int mini = Getmid(a, begin, end);
	swap(&a[begin], &a[mini]);
	int key = a[begin];
	int hole = begin;
	int left = begin, right = end;
	while (left < right)
	{
		while (left < right && a[right] >= key)
			right--;
		a[hole] = a[right];
		hole = right;
		while (left < right && a[left] <= key)
			left++;
		a[hole] = a[left];
		hole = left;
	}
	a[hole] = key;
	return hole;
}

int PartSort3(int* a, int begin, int end)
{
	int midi = Getmid(a, begin, end);
	swap(&a[begin], &a[midi]);
	int prev = begin;
	int cur = prev + 1;
	int key = begin;
	while (cur <= end)
	{
		if (a[cur]<a[key]&&++prev!=cur)
			swap(&a[prev], &a[cur]);
		cur++;
	}
	swap(&a[prev], &a[key]);
	return prev;
}

void QuckilySort(int* a, int begin, int end)
{
	if (begin >= end)
		return;
	//int keyi = PartSort1(a, begin, end);
	int keyi = PartSort2(a, begin, end);
	//int keyi = PartSort3(a, begin, end);
	QuckilySort(a, begin, keyi - 1);
	QuckilySort(a, keyi + 1, end);
}

int main()
{
	int a[10] = { 1,2,5,4,3,6,8,7,9,10 };
	int n = sizeof(a) / sizeof(a[0]);
	//BubbleSort(a, n);
	//SelectSort(a, n);
	//InsertSort(a, n);
	//ShellSort(a, n);
	//DuiSort(a, n);
	//QuckilySort(a, 0, n-1);
	PrintArray(a, n);
}